package com.btm.easymetadata.server.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.btm.easymetadata.server.entity.base.Response;
import com.btm.easymetadata.server.entity.dto.LinkDataFieldDTO;
import com.btm.easymetadata.server.entity.vo.DataFieldVO;
import com.btm.easymetadata.server.service.LinkFieldService;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@AllArgsConstructor
@RequestMapping("/link/field")
public class LinkFieldController {

    private final LinkFieldService linkFieldService;

    @GetMapping("/by_from")
    public Response<List<DataFieldVO>> selectByFrom(@RequestParam("fieldId") Long filedId) {
        Response<List<DataFieldVO>> response = new Response<>();
        response.setCode(200);
        response.setData(linkFieldService.selectByFrom(filedId));
        return response;
    }

    @GetMapping("/by_to")
    public Response<List<DataFieldVO>> selectByTo(@RequestParam("fieldId") Long filedId) {
        Response<List<DataFieldVO>> response = new Response<>();
        response.setCode(200);
        response.setData(linkFieldService.selectByTo(filedId));
        return response;
    }

    @GetMapping("/add")
    public Response<Boolean> addLink(@RequestParam("fromId") Long fromId, @RequestParam("toId") Long toId) {
        Response<Boolean> response = new Response<>();
        response.setCode(200);
        response.setData(linkFieldService.addLink(fromId, toId));
        return response;
    }

    /**
     * 查询与当前字段无直接或间接关联的数据
     * @return
     */
    @PostMapping("/linkable")
    public Response<List<DataFieldVO>> selectLinkable(@RequestBody LinkDataFieldDTO linkDataFieldDTO) {
        Response<List<DataFieldVO>> response = new Response<>();
        response.setCode(200);
        response.setData(linkFieldService.selectLinkable(linkDataFieldDTO));
        return response;
    }
}
